#include "fw_hal.h"
#include "userdef.h"

#define DEBUG_EEPROM 0

uint8_t eeprom_read(uint8_t *dat,uint16_t addr, uint8_t _size)
{
	uint8_t i;
	IAP_SetEnabled(HAL_State_ON);
#if DEBUG_EEPROM
	UART1_TxString("\r\n");
#endif	
	for(i=0;i<_size;i++)
	{
		IAP_CmdRead(addr + i);
		if(IAP_IsCmdFailed()){
			IAP_ClearCmdFailFlag();
			return 1;
		}else
		{
			dat[i] =IAP_ReadData();
		}
#if DEBUG_EEPROM
		UART1_TxString("r:");
		debug_u32(dat[i]);
    UART1_TxString(" ");
#endif		
	}
	IAP_SetEnabled(HAL_State_OFF);
	return 0;
}

uint8_t eeprom_write(uint8_t *dat,uint16_t addr, uint8_t _size)
{
	uint8_t i;
#if DEBUG_EEPROM	
	IAP_SetEnabled(HAL_State_ON);
#endif	
	for(i=0;i<_size;i++)
	{
		IAP_WriteData(dat[i]);
		IAP_CmdWrite(addr + i);
		if(IAP_IsCmdFailed()){
			IAP_ClearCmdFailFlag();
			return 1;
		}
#if DEBUG_EEPROM			
		UART1_TxString("w:");
		debug_u32(dat[i]);
		UART1_TxString(" ");
#endif		
	}
	IAP_SetEnabled(HAL_State_OFF);
	return 0;
}
